package com.yunxin.server.config;


import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
//import com.yunxin.gateway.infra.constant.GatewayConstants;
import com.yunxin.server.constant.GatewayConstants;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Swagger 配置
 * <p>
 * Spring Doc FAQ
 *
 * @author zero°
 * @since 2023-05-22
 */
@Configuration
public class SwaggerConfig {

    /**
     * 接口信息
     */
    @Bean
    public OpenAPI apiInfo() {
        return new OpenAPI().info(new Info()
                .title("云信管理后台 接口文档")
                .version("1.0.0")
                .description("接口文档")
                .license(new License().name("Apache 2.0")));
    }

    /**
     * 接口分组-系统接口
     *
     * @return GroupedOpenApi
     */
    @Bean
    public GroupedOpenApi groupedOpenApi() {
        String[] paths = {"/api/**"};
        String[] packagesToScan = {"com.yunxin.admin.system.api.system","com.yunxin.admin.system.api.warn"};
        return GroupedOpenApi.builder().group("系统接口(管理后台)")
                .packagesToScan(packagesToScan)
                .pathsToMatch(paths)
                .addOperationCustomizer(admintOperationCustomizer())
                .build();
    }




    /**
     * 接口分组-核心业务接口
     *
     * @return GroupedOpenApi
     */
    @Bean
    public GroupedOpenApi groupedCoreApi() {
        String[] paths = {"/core/**"};
        String[] packagesToScan = {"com.yunxin.admin.system.api.core","com.yunxin.admin.system.api.statistics","com.yunxin.admin.system.api.system"};
        return GroupedOpenApi.builder().group("核心业务接口(管理后台)")
                .packagesToScan(packagesToScan)
                .pathsToMatch(paths)
                .addOperationCustomizer(admintOperationCustomizer())
                .build();
    }

    /**
     * 接口分组-微信回调模块接口
     *
     * @return GroupedWechatApi
     */
    @Bean
    public GroupedOpenApi groupedWechatApi() {
        String[] paths = {"/callback/**"};
        String[] packagesToScan = {"com.yunxin.admin.system.api.wechat"};
        return GroupedOpenApi.builder().group("微信回调模块")
                .packagesToScan(packagesToScan)
                .pathsToMatch(paths)
                //.addOperationCustomizer(gatewayOperationCustomizer())
                .build();
    }

    /**
     * 接口分组-导出接口
     *
     * @return GroupedOpenApi
     */
    @Bean
    public GroupedOpenApi groupedDownloadApi() {
        String[] paths = {"/download/**"};
        String[] packagesToScan = {"com.yunxin.admin.system.api.download"};
        return GroupedOpenApi.builder().group("导出接口(管理后台)")
                .packagesToScan(packagesToScan)
                .pathsToMatch(paths)
                .pathsToExclude("/download/addMethodConfig", "/download/updateMethodConfig")
                .addOperationCustomizer(admintOperationCustomizer())
                .build();
    }


    /**
     * admin 管理后台接口 接口认证配置
     * @return OpenApiCustomizer
     */
    @Bean
    public OperationCustomizer admintOperationCustomizer() {
        return (operation, handlerMethod) -> operation.addParametersItem(new Parameter()
                .in(ParameterIn.HEADER.toString())
                .required(true)
                .description("token 验证")
                .name(SecurityConstants.TOKEN_KEY)
                .example(SecurityConstants.TOKEN_PREFIX));
    }

    /**
     * gateway 网关接口 接口认证配置
     * @return OpenApiCustomizer
     */
    @Bean
    public OperationCustomizer gatewayOperationCustomizer() {
        return (operation, handlerMethod) -> operation.addParametersItem(new Parameter()
                .in(ParameterIn.HEADER.toString())
                .required(true)
                .description("token 验证")
                .name(GatewayConstants.JSON_WEB_TOKEN_KEY));
    }



}
